package org.jeecg.modules.zcgl.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.zcgl.entity.RegionUser;
import org.jeecg.modules.zcgl.service.IRegionUserService;
import org.jeecg.modules.zcgl.vo.RegionUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 区域管理员信息
 * @Author: jeecg-boot
 * @Date:   2020-08-11
 * @Version: V1.0
 */
@Api(tags="区域管理员信息")
@RestController
@RequestMapping("/zcgl/regionUser")
@Slf4j
public class RegionUserController {
	@Autowired
	private IRegionUserService regionUserService;

	@Autowired
	private ISysUserService sysUserService;


	/**
	 * 分页列表查询
	 * @param username
	 * @param regionId
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "区域管理员信息-分页列表查询")
	@ApiOperation(value="区域管理员信息-分页列表查询", notes="区域管理员信息-分页列表查询")
	@GetMapping(value = "/listUserByRegionId")
	public Result<IPage<SysUser>> queryUserPageListByRegionId(@RequestParam(name="username",required=false) String username,
															  @RequestParam(name="regionId",required=true) String regionId,
															  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
															  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
															  HttpServletRequest req) {
		Page<SysUser> page = new Page<SysUser>(pageNo, pageSize);
		if (StringUtils.isBlank(regionId)){
			return Result.ok(page);
		}
		List<String> ids = regionUserService.queryUserIdListByRegionId(regionId);
		if (ids == null || ids.size() == 0){

			return Result.ok(page);
		}
		LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.in(SysUser::getId, ids);
		if (StringUtils.isNotBlank(username)) {
			queryWrapper.like(SysUser::getUsername, username);
		}
		IPage<SysUser> pageList = sysUserService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

	 /**
	  *
	  * @param vo
	  * @return
	  */
	@AutoLog(value = "区域管理员信息-添加")
	@ApiOperation(value="区域管理员信息-添加", notes="区域管理员信息-添加")
	@PostMapping(value = "/addByRegionId")
	public Result<?> addByRegionId(@RequestBody RegionUserVo vo) {
		boolean bool = regionUserService.addList(vo);
		return Result.ok();
	}

	 /**
	  * 删除
	  * @param userId
	  * @param regionId
	  * @return
	  */
	@AutoLog(value = "区域管理员信息-通过id删除")
	@ApiOperation(value="区域管理员信息-通过id删除", notes="区域管理员信息-通过id删除")
	@DeleteMapping(value = "/deleteByRegionId")
	public Result<?> deleteByRegionId(@RequestParam(name="userId",required=true) String userId,
									  @RequestParam(name="regionId",required=true) String regionId) {
		LambdaQueryWrapper<RegionUser> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(RegionUser::getRegionId, regionId);
		queryWrapper.eq(RegionUser::getUserId, userId);
		regionUserService.remove(queryWrapper);
		return Result.ok("删除成功!");
	}

	 /**
	  *
	  *  批量删除
	  * @param userIds
	  * @param regionId
	  * @return
	  */
	@AutoLog(value = "区域管理员信息-批量删除")
	@ApiOperation(value="区域管理员信息-批量删除", notes="区域管理员信息-批量删除")
	@DeleteMapping(value = "/deleteBatchByRegionId")
	public Result<?> deleteBatchByRegionId(@RequestParam(name="userIds",required=true) String userIds,
										   @RequestParam(name="regionId",required=true) String regionId) {
		LambdaQueryWrapper<RegionUser> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(RegionUser::getRegionId, regionId);
		queryWrapper.in(RegionUser::getUserId, Arrays.asList(userIds.split(CommonConstant.DEFAULT_SEPARATOR)));
		regionUserService.remove(queryWrapper);
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "区域管理员信息-通过id查询")
	@ApiOperation(value="区域管理员信息-通过id查询", notes="区域管理员信息-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		RegionUser regionUser = regionUserService.getById(id);
		if(regionUser==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(regionUser);
	}

}
